import {MAP_CONFIG} from "../config/config";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Background extends cc.Component {
  gridWidth = 64;

  @property({type: cc.Texture2D})
  bgTopLeft: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bgTopRight: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bgBottomRight: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bgBottomLeft: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bgTop: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bgLeft: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bgBottom: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bgRight: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bgCenter: cc.Texture2D = null

  @property({type: cc.Texture2D})
  topTexture_0: cc.Texture2D = null

  @property({type: cc.Texture2D})
  topTexture_1: cc.Texture2D = null

  @property({type: cc.Texture2D})
  leftTexture_0: cc.Texture2D = null

  @property({type: cc.Texture2D})
  leftTexture_1: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bottomTexture_0: cc.Texture2D = null

  @property({type: cc.Texture2D})
  bottomTexture_1: cc.Texture2D = null

  @property({type: cc.Texture2D})
  fish1: cc.Texture2D = null

  @property({type: cc.Texture2D})
  fish2: cc.Texture2D = null

  @property({type: cc.Texture2D})
  fish3: cc.Texture2D = null

  width = MAP_CONFIG.column;

  height = MAP_CONFIG.row;

  bottomWidth = 652;

  BLWidth = 405;

  BLHeight = 273;

  leftHeight = 342;

  onLoad() {
    const topLeftY = this.leftHeight * this.height + this.BLHeight;
    const bottomRightX = this.bottomWidth * this.width + this.BLWidth;


    const spriteBL = this.createSprite(this.bgBottomLeft)
    this.node.addChild(spriteBL.node);
    for (let i = 0; i < this.width; i++) {
      const x = this.BLWidth + i * this.bottomWidth;
      const spriteBottom = this.createSprite(this.bgBottom)
      spriteBottom.node.x = x;
      this.node.addChild(spriteBottom.node)
      const bottomDecorator = this.createSprite(this['bottomTexture_' + i % 2]);
      bottomDecorator.node.x = x;
      this.node.addChild(bottomDecorator.node)

      const spriteT = this.createSprite(this.bgTop)
      spriteT.node.x = x;
      spriteT.node.y = topLeftY;
      const topDecorator = this.createSprite(this['topTexture_' + i % 2]);
      if (i % 2 === 1) {
        topDecorator.node.x = x + 300;
        topDecorator.node.y = topLeftY + 80;
      } else {
        topDecorator.node.x = x;
        topDecorator.node.y = topLeftY;
      }
      this.node.addChild(spriteT.node)
      this.node.addChild(topDecorator.node)

    }
    const spriteBR = this.createSprite(this.bgBottomRight)
    spriteBR.node.x = bottomRightX;
    this.node.addChild(spriteBR.node)

    for (let i = 0; i < this.height; i++) {
      const y = this.BLHeight + i * this.leftHeight;
      const spriteL = this.createSprite(this.bgLeft)
      spriteL.node.y = y
      this.node.addChild(spriteL.node)
      const leftDecorator = this.createSprite(this['leftTexture_' + i % 2]);
      if (i % 2 === 1) {
        leftDecorator.node.y = y;
      } else {
        leftDecorator.node.y = y;
      }
      leftDecorator.node.x = 0;
      this.node.addChild(leftDecorator.node)

      const spriteR = this.createSprite(this.bgRight)
      spriteR.node.x = bottomRightX;
      spriteR.node.y = y;
      this.node.addChild(spriteR.node);
    }
    const spriteTL = this.createSprite(this.bgTopLeft)
    spriteTL.node.y = topLeftY;
    this.node.addChild(spriteTL.node);

    const spriteTR = this.createSprite(this.bgTopRight)
    spriteTR.node.x = bottomRightX;
    spriteTR.node.y = topLeftY;
    this.node.addChild(spriteTR.node)

    for (let i = 0; i < this.width; i++) {
      for (let j = 0; j < this.height; j++) {
        const spriteCenter = this.createSprite(this.bgCenter);
        const x = this.BLWidth + i * this.bottomWidth;
        const y = this.BLHeight + j * this.leftHeight;
        spriteCenter.node.x = x;
        spriteCenter.node.y = y;
        this.node.addChild(spriteCenter.node)
      }
    }
  }

  private createSprite(texture: cc.Texture2D): cc.Sprite {
    const spriteFrame = new cc.SpriteFrame(texture)
    const node = new cc.Node()
    node.addComponent(cc.Sprite)
    node.anchorX = 0;
    node.anchorY = 0;
    const sprite = node.getComponent(cc.Sprite)
    sprite.spriteFrame = spriteFrame
    return sprite;
  }

}